//
//  UIAlertExtension.swift
//  swiftNetworkTest
//
//  Created by redjun Z on 2019/1/23.
//  Copyright © 2019 network.xiaozhang.com. All rights reserved.
//

import Foundation
import UIKit

class UIAlertExtension: UIViewController {
    
    typealias currentAction = (UIButton)-> Void
    
    var showView: UIView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if let view = showView {
            self.view.addSubview(view)
        }
    }
    
    // MARK:-- 初始化
    init(showView view: UIView) {
        super.init(nibName: nil, bundle: nil)
        showView = view
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:-- 弹出\消失
    /// 点击空白地方 dismiss 控制器
    ///
    /// - Parameters:
    ///   - touches: 手势集合
    ///   - event: 对象
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
        UIAlertExtension.dismissAlert(currentVc: self, animated: false, completion: nil)
    }
    
    //    类方法
    class func dismissAlert(currentVc :UIViewController,animated flag: Bool, completion: (() -> Void)? = nil) {
        currentVc.dismiss(animated: flag) {
            UIAlertController.topViewController?.tabBarController?.tabBar.isHidden = false
        }
    }
    //    实例方法
    func dismissAlert(flag: Bool, completion: (() -> Void)? = nil) {
        dismiss(animated: flag) {
            UIAlertController.topViewController?.tabBarController?.tabBar.isHidden = false
        }
    }
    
    
    /// 模态弹框
    ///
    /// - Parameters:
    ///   - viewControllerToPresent: 试图控制器
    ///   - flag: 是否显示动画
    ///   - completion: 钩子
    class func presentVc(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
        viewControllerToPresent.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
        viewControllerToPresent.view.backgroundColor = UIColor.init(red: CGFloat(0)/255.0, green: CGFloat(0)/255.0, blue: CGFloat(0)/255.0, alpha: 0.5)
        UIAlertController.topViewController?.modalPresentationStyle = UIModalPresentationStyle.currentContext
        UIAlertController.topViewController?.tabBarController?.tabBar.isHidden = true; // 隐藏tabBar
        UIAlertController.topViewController?.present(viewControllerToPresent, animated: flag, completion: completion)
    }
    
    // MARK:-- 系统弹框
    
    /// 系统的提示框
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - message: 消息内容
    ///   - alertButtons: 按钮数组
    ///   - preferredStyle: 样式
    ///   - callback: 弹框展示完成以后的 钩子
    /// - Returns: 当前弹框对象
    class func showSystemAlert(title:String,message: String,alertButtons: [UIAlertAction],preferredStyle: UIAlertController.Style,callback:(()->Void)? = nil) -> UIAlertController {
        let alerta = UIAlertController(title: title, message: message, preferredStyle: preferredStyle)
        for btn in alertButtons {
            alerta.addAction(btn)
        }
        UIAlertController.topViewController?.present(alerta, animated: true) {
            callback?()
        }
        return alerta
    }
    
    
    /// 系统提示框 带输入框
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - message: 消息内容
    ///   - alertButtons: 按钮数组
    ///   - textFieldCall: 输入框 配置
    ///   - callback: 展示完成 钩子
    /// - Returns: 当前弹框对象
    
    func prompt(_ message: String, title: String! = nil, text: String? = nil, placeholder: String? = "请输入", completion: @escaping (String?) -> Void) {
        let controller = UIAlertController(title: title, message: message, preferredStyle: .alert)
        controller.addAction(UIAlertAction(title: "取消", style: .cancel) { action in
            controller.dismiss(animated: true, completion: nil)
            completion(nil)
        })
        controller.addAction(UIAlertAction(title: "确定", style: .default) { action in
            controller.dismiss(animated: true, completion: nil)
            completion(controller.textFields?[0].text ?? "")
        })
        controller.addTextField { textField in
            if let value = text {
                textField.text = value
            }
            if let ph = placeholder {
                textField.placeholder = ph
            }
        }
        UIViewController.topViewController?.present(controller, animated: true, completion: nil)
    }
    
    
    class func showTextFieldAlert(title:String,message: String,sureButtonTitle: String?,otherButtons: [UIAlertAction]?,textFieldCall:@escaping (_ textField: UITextField)->(),callback:@escaping (String?) -> Void) {
        let alerta = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alerta.addAction(UIAlertAction(title: sureButtonTitle ?? "确定", style: .default) { action in
            alerta.dismiss(animated: true, completion: nil)
            callback(alerta.textFields?[0].text ?? "")
        })
        for btn in otherButtons ?? [UIAlertAction]() {
            alerta.addAction(btn)
        }
        alerta.addTextField { (textField) in
            textFieldCall(textField)
        }
        UIAlertController.topViewController?.present(alerta, animated: true, completion: nil)
    }
    
    // MARK:-- 自定义弹框
    ///  自定义的弹框
    ///
    /// - Parameters:
    ///   - showBack: 展示完成的回调
    ///   - callBack: 点击视图中 button按钮的回调
    /// - Returns: 返回当前视图控制器
    func showCustomAlertView(showBack:@escaping ()->(),buttonAction callBack:@escaping currentAction) -> UIAlertExtension {
        for subView in self.showView?.subviews ?? UIView().subviews {
            if(subView.isKind(of: UIButton.classForCoder())){
                (subView as! UIButton).fk_addAction { [weak self] (btn) in
                    self?.dismissAlert(flag: false)
                    callBack(btn)
                }
            }
        }
        UIAlertExtension.presentVc(self, animated: true, completion: showBack)
        return self
    }
    
}


